package edu.ccut.titan.tag.chart;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.jsp.JspException;

import org.jfree.chart.plot.PlotOrientation;

import edu.ccut.titan.tag.chart.utils.ChartInfo;
import edu.ccut.titan.tag.chart.utils.SubAixs;

public class TitanChartTag extends TitanAbstractChartTag {

	private static final long serialVersionUID = 4126410061971706936L;

	private static final String CHART_TAG_KEY = "chart_tag";

	private String type = ChartInfo.BAR_CHART;

	private String title = "";

	private String subTitle = "";

	private String categoryAxisLable = "";
	
	private String categoryAxisLablePosition = "";

	private String numberAxisLable = "";
	
	private boolean legend = true;

	private boolean tooltips = true;

	private boolean urls = false;

	private Object value;

	private String orientation = "VERTICAL";

	private List<SubAixs> subAixses = new ArrayList<SubAixs>();
	
	public TitanChartTag() {
		super(CHART_TAG_KEY);
	}

	@SuppressWarnings("unchecked")
	public ChartInfo getChartInfo() {
		ChartInfo chartInfo = new ChartInfo();

		chartInfo.setType(this.type);
		chartInfo.setTitle(this.title);
		chartInfo.setSubTitle(this.subTitle);
		chartInfo.setCategoryAxisLable(this.categoryAxisLable);
		chartInfo.setNumberAxisLable(this.numberAxisLable);
		chartInfo.setLegend(this.legend);
		chartInfo.setTooltips(this.tooltips);
		chartInfo.setUrls(this.urls);
		chartInfo.setSubAixses(this.subAixses);
		chartInfo.setCategoryAxislablePosition(categoryAxisLablePosition);

		if (this.value instanceof List) {
			chartInfo.setDatas((List<List<?>>) this.value);
		}

		if ("VERTICAL".equalsIgnoreCase(this.orientation)) {
			chartInfo.setOrientation(PlotOrientation.VERTICAL);
		} else {
			chartInfo.setOrientation(PlotOrientation.HORIZONTAL);
		}
		
		if (super.getWidth() != null && super.getWidth().length() > 0) {
			chartInfo.setWidth(Integer.parseInt(super.getWidth()));
		}
		
		if (super.getHeight() != null && super.getHeight().length() > 0) {
			chartInfo.setHeight(Integer.parseInt(super.getHeight()));
		}

		return chartInfo;
	}

	public int doStartTag() {
		this.subAixses.clear();
		
		return EVAL_BODY_INCLUDE;
	}
	
	public int doEndTag() throws JspException {
		super.writeChart(this.getChartInfo());

		return SKIP_BODY;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getSubTitle() {
		return subTitle;
	}

	public void setSubTitle(String subTitle) {
		this.subTitle = subTitle;
	}

	public String getCategoryAxisLable() {
		return categoryAxisLable;
	}

	public void setCategoryAxisLable(String categoryAxisLable) {
		this.categoryAxisLable = categoryAxisLable;
	}

	public String getNumberAxisLable() {
		return numberAxisLable;
	}

	public void setNumberAxisLable(String numberAxisLable) {
		this.numberAxisLable = numberAxisLable;
	}

	public boolean isLegend() {
		return legend;
	}

	public void setLegend(boolean legend) {
		this.legend = legend;
	}

	public boolean isTooltips() {
		return tooltips;
	}

	public void setTooltips(boolean tooltips) {
		this.tooltips = tooltips;
	}

	public boolean isUrls() {
		return urls;
	}

	public void setUrls(boolean urls) {
		this.urls = urls;
	}

	public Object getValue() {
		return value;
	}

	public void setValue(Object value) {
		if (value instanceof String) {
			this.value = super.getExpressValueObject((String) value);
		} else {
			this.value = value;
		}
	}

	public String getOrientation() {
		return orientation;
	}

	public void setOrientation(String orientation) {
		this.orientation = orientation;
	}

	public List<SubAixs> getSubAixses() {
		return subAixses;
	}

	public String getCategoryAxisLablePosition() {
		return categoryAxisLablePosition;
	}

	public void setCategoryAxisLablePosition(String categoryAxisLablePosition) {
		this.categoryAxisLablePosition = categoryAxisLablePosition;
	}
}
